#pragma once
#include <iostream>
#include "protocol.hpp"
enum
{
    Div_Err = 1,
    Mod_Err,
    UnKnow
};
class ServerCal
{
public:
    ServerCal() {}
    ~ServerCal() {}

    Response CalculatorHelper(Request &req)
    {
        Response resp(0, 0);
        switch (req.op_)
        {
        case '+':
            resp.result_ = req.x_ + req.y_;
            break;
        case '-':
            resp.result_ = req.x_ - req.y_;
            break;
        case '*':
            resp.result_ = req.x_ * req.y_;
            break;
        case '/':
        {
            if (req.y_ == 0)
                resp.code_ = Div_Err;
            else
                resp.result_ = req.x_ / req.y_;
        }
        break;
        case '%':
        {
            if (req.y_ == 0)
                resp.code_ = Mod_Err;
            else
                resp.result_ = req.x_ % req.y_;
        }
        break;
        default:
            resp.code_ = UnKnow;
            break;
        }
        return resp;
    }
    std::string Calculator(std::string &package)
    {
        std::string content;
        bool r = Deable(package, &content);
        if (!r)
        {
            return "";
        }
        Request req;
        req.Deserialize(content);

        Response resp = CalculatorHelper(req);
        content = "";
        r = resp.Serialize(&content);
        if (!r)
        {
            return "";
        }
        content = Enable(content);
        return content;
    }
};